/*     */ package com.zimbra.cs.session;
/*     */ 
/*     */ import com.google.common.base.Objects;
/*     */ import com.google.common.base.Objects.ToStringHelper;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.mailbox.MailItem.Type;
/*     */ import com.zimbra.cs.mailbox.MailServiceException;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.MailboxLock;
/*     */ import com.zimbra.cs.mailbox.MailboxManager;
/*     */ import com.zimbra.cs.mailbox.MailboxManager.FetchMode;
/*     */ import com.zimbra.cs.service.util.SyncToken;
/*     */ import java.util.Set;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class WaitSetAccount
/*     */ {
/*     */   private String accountId;
/*     */   private Set<MailItem.Type> interests;
/*     */   private SyncToken lastKnownSyncToken;
/*     */   private String sessionId;
/*     */   
/*     */   public WaitSetAccount(String id, SyncToken sync, Set<MailItem.Type> interest)
/*     */   {
/*  37 */     setAccountId(id);
/*  38 */     this.lastKnownSyncToken = sync;
/*  39 */     this.interests = interest;
/*     */   }
/*     */   
/*     */   public WaitSetSession getSession() {
/*  43 */     if (this.sessionId != null) {
/*     */       try {
/*  45 */         Mailbox mbox = getMailboxIfLoaded();
/*  46 */         if (mbox != null)
/*  47 */           return (WaitSetSession)mbox.getListener(this.sessionId);
/*     */       } catch (ServiceException e) {
/*  49 */         ZimbraLog.session.info("Caught exception fetching mailbox in WaitSetAccount.getSession()", e);
/*     */       }
/*     */     }
/*  52 */     return null;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public WaitSetError createSession(Mailbox mbox, SomeAccountsWaitSet ws)
/*     */   {
/*  70 */     WaitSetSession session = new WaitSetSession(ws, this.accountId, this.interests, this.lastKnownSyncToken);
/*  71 */     mbox.lock.lock();
/*     */     try {
/*  73 */       session.register();
/*  74 */       this.sessionId = session.getSessionId();
/*     */       
/*  76 */       session.update(this.interests, this.lastKnownSyncToken);
/*     */     } catch (MailServiceException e) {
/*  78 */       this.sessionId = null;
/*  79 */       if (e.getCode().equals("mail.MAINTENANCE"))
/*     */       {
/*     */ 
/*  82 */         ZimbraLog.session.debug("Maintenance mode trying to initialize WaitSetSession for accountId " + this.accountId);
/*     */       } else {
/*  84 */         ZimbraLog.session.warn("Error initializing WaitSetSession for accountId " + this.accountId + " -- MailServiceException", e);
/*  85 */         return new WaitSetError(this.accountId, WaitSetError.Type.ERROR_LOADING_MAILBOX);
/*     */       }
/*     */     } catch (ServiceException e) { WaitSetError localWaitSetError;
/*  88 */       this.sessionId = null;
/*  89 */       ZimbraLog.session.warn("Error initializing WaitSetSession for accountId " + this.accountId + " -- ServiceException", e);
/*  90 */       if (e.getCode() == "account.NO_SUCH_ACCOUNT")
/*  91 */         return new WaitSetError(this.accountId, WaitSetError.Type.NO_SUCH_ACCOUNT);
/*  92 */       if (e.getCode() == "service.WRONG_HOST") {
/*  93 */         return new WaitSetError(this.accountId, WaitSetError.Type.WRONG_HOST_FOR_ACCOUNT);
/*     */       }
/*  95 */       return new WaitSetError(this.accountId, WaitSetError.Type.ERROR_LOADING_MAILBOX);
/*     */     }
/*     */     finally {
/*  98 */       mbox.lock.release();
/*     */     }
/* 100 */     return null;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public void cleanupSession()
/*     */   {
/* 107 */     WaitSetSession session = getSession();
/* 108 */     if (session != null) {
/* 109 */       this.sessionId = null;
/* 110 */       session.doCleanup();
/*     */     }
/*     */   }
/*     */   
/*     */   public String toString()
/*     */   {
/* 116 */     return Objects.toStringHelper(this).add("account", this.accountId).toString();
/*     */   }
/*     */   
/*     */   private Mailbox getMailboxIfLoaded() throws ServiceException {
/* 120 */     Mailbox mbox = MailboxManager.getInstance().getMailboxByAccountId(this.accountId, MailboxManager.FetchMode.ONLY_IF_CACHED);
/*     */     
/* 122 */     return mbox;
/*     */   }
/*     */   
/*     */   void setLastKnownSyncToken(SyncToken lastKnownSyncToken) {
/* 126 */     this.lastKnownSyncToken = lastKnownSyncToken;
/*     */   }
/*     */   
/*     */   public SyncToken getLastKnownSyncToken() {
/* 130 */     return this.lastKnownSyncToken;
/*     */   }
/*     */   
/*     */   void setAccountId(String accountId) {
/* 134 */     this.accountId = accountId;
/*     */   }
/*     */   
/*     */   public String getAccountId() {
/* 138 */     return this.accountId;
/*     */   }
/*     */   
/*     */   void setInterests(Set<MailItem.Type> interests) {
/* 142 */     this.interests = interests;
/*     */   }
/*     */   
/*     */   public Set<MailItem.Type> getInterests() {
/* 146 */     return this.interests;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/session/WaitSetAccount.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */